/*
 * Utils.cpp
 */

#include "Utils.h"
#include <ctime>
#include <cstdlib>
#include <iostream>

string* Utils::stringTokenize(string str, char delimiter, int& tokenCount) {
	string* tokens = new string[5];
	string token = "";
	int j = 0;
	for (unsigned int i = 0; i < str.size() ; i++) {
		if (str[i] == delimiter && j < 4) {
			if (token.size() > 0) {
				tokens[j] = token;
				j++;
				tokenCount++;
				token = "";
			}
		} else {
			token = token + str[i];
		}
	}
	if (token.size() > 0) {
		tokens[j] = token;
		tokenCount++;
	}
	return tokens;
}

float Utils::stringToFloat(string s){
	float value;
	istringstream str(s);
	str >> value;
	return value;
}

int Utils::stringToInt(string s){
	istringstream buffer(s);
	int value;
	buffer >> value;
	return value;
}

bool Utils::validateFloat(string s) {
	int value;
	int dots = 0;
	for (unsigned int i = 0; i < s.size(); i++){
		value = (int)s[i];
		if (((value < 44) || (value > 57) || (value == 47)) || ((value == 45) && (i > 0))) return false;
		if ((value == 44) || (value == 46)) dots++;
		if (dots > 1) return false; 
	}
	return true;
}

void Utils::comaToDot(string& s) {
		for (unsigned int i = 0; i < s.size(); i++){
			if (s[i] == ',') s[i] = '.';
		}
}

string Utils::negativeToPositive(string s) {
	string str = "";
	for (unsigned int i = 0; i < (s.size()-1); i++){
		str = str + s[i+1];
	}
return str;
}
string Utils::intToString(int value){
	string s;
	stringstream ret;
	ret << value;
	s = ret.str();
	return s;
}

bool Utils::validateInteger(string s)
{
	int value;
	for (unsigned int i = 0; i < s.size(); i++){
		value = (int)s[i];
		if (value < 48 || value > 57){
			return false;
		}
	}
	return true;
}

int Utils::factorial(int n)
{
	if(n < 0){
		return 0;
	} else {
		if(n > 1){
			return n*Utils::factorial(n-1);
		}
		return 1;
	}
}

int Utils::random(int from, int to)
{
	srand((unsigned)time(0));
	return from + (rand()%(to+1));
}






